import tkinter as tk
from tkinter import scrolledtext, messagebox
import configparser
import pyperclip

config_file = 'replacement_rules.ini'

# 加载配置文件
def load_replacement_rules():
    config = configparser.ConfigParser()
    config.read(config_file, encoding='utf-8')
    if '替换规则' not in config:
        config['替换规则'] = {}
    return config

# 保存配置文件
def save_replacement_rules(config):
    with open(config_file, 'w', encoding='utf-8') as file:
        config.write(file)

# 更新右侧的配置文件列表显示
def update_rules_list():
    rules_list.delete(0, tk.END)
    for src_word, target_word in replacement_rules['替换规则'].items():
        rules_list.insert(tk.END, f"{src_word} -> {target_word}")

# 添加新的替换规则
def add_replacement_rule():
    src_word = source_word_entry.get().strip()
    target_word = target_word_entry.get().strip()

    if not src_word or not target_word:
        messagebox.showwarning("输入错误", "请同时输入源词和目标词！")
        return

    # 添加到配置中
    replacement_rules['替换规则'][src_word] = target_word
    save_replacement_rules(replacement_rules)

    # 清空输入框
    source_word_entry.delete(0, tk.END)
    target_word_entry.delete(0, tk.END)

    # 更新规则列表
    update_rules_list()

# 执行替换操作
def replace_words():
    text_to_replace = input_text.get("1.0", tk.END)
    for src_word, target_word in replacement_rules['替换规则'].items():
        text_to_replace = text_to_replace.replace(src_word, target_word)

    output_text.delete("1.0", tk.END)
    output_text.insert(tk.END, text_to_replace)

# 复制输出文本
def copy_output():
    output = output_text.get("1.0", tk.END).strip()
    if output:
        pyperclip.copy(output)
        messagebox.showinfo("复制成功", "替换后的文本已复制到剪贴板！")
    else:
        messagebox.showwarning("无内容", "没有可复制的内容，请先进行替换！")

# 初始化配置文件
replacement_rules = load_replacement_rules()

# 创建主窗口
window = tk.Tk()
window.title("批量替换文字工具")
window.geometry("900x600")

# 源词与目标词输入框和添加按钮
tk.Label(window, text="源词:").grid(row=0, column=0, sticky="e")
source_word_entry = tk.Entry(window, width=30)
source_word_entry.grid(row=0, column=1, padx=10, pady=5)

tk.Label(window, text="目标词:").grid(row=1, column=0, sticky="e")
target_word_entry = tk.Entry(window, width=30)
target_word_entry.grid(row=1, column=1, padx=10, pady=5)

add_button = tk.Button(window, text="添加替换词", command=add_replacement_rule)
add_button.grid(row=1, column=2, padx=10, pady=5)

# 配置文件列表显示
tk.Label(window, text="当前替换规则:").grid(row=0, column=3, rowspan=2, padx=10)
rules_list = tk.Listbox(window, width=40, height=15)
rules_list.grid(row=0, column=4, rowspan=6, padx=10, pady=5, sticky="n")

# 更新初始显示的规则
update_rules_list()

# 待替换文本输入框
tk.Label(window, text="待替换文本:").grid(row=3, column=0, columnspan=2)
input_text = scrolledtext.ScrolledText(window, wrap=tk.WORD, width=50, height=10)
input_text.grid(row=4, column=0, columnspan=3, padx=10, pady=5)

# 替换按钮
replace_button = tk.Button(window, text="替换", command=replace_words)
replace_button.grid(row=5, column=1, pady=10)

# 输出文本框
tk.Label(window, text="替换后的文本:").grid(row=6, column=0, columnspan=2)
output_text = scrolledtext.ScrolledText(window, wrap=tk.WORD, width=50, height=10)
output_text.grid(row=7, column=0, columnspan=3, padx=10, pady=5)

# 复制按钮
copy_button = tk.Button(window, text="复制结果", command=copy_output)
copy_button.grid(row=8, column=1, pady=10)

# 运行主循环
window.mainloop()
